﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace TaiDouPhotonServer.DB
{
    public class NHiberateHelper
    {
        private static ISessionFactory sessionFactory = null;

        private static void InitializeSessionFactory()
        {
            MySQLConfiguration config = MySQLConfiguration.Standard.ConnectionString(
                db => db.Server("localhost").Database("taidouserver").Username("root").Password("bili"));
            sessionFactory = Fluently.Configure().Database(config).Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHiberateHelper>()).BuildSessionFactory();
        }

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                {
                    InitializeSessionFactory();
                }

                return sessionFactory;
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
